package com.code.app.loading;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class ShapeView extends View {

    private Shap shap = Shap.TRIANGLE;

    private Paint circlePaint;

    private Paint rectFPaint;

    private Paint trianglePaint;

    public ShapeView(Context context) {
        super(context);
    }

    public ShapeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ShapeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        circlePaint = new Paint();
        circlePaint.setColor(Color.YELLOW);
        circlePaint.setAntiAlias(true);

        rectFPaint = new Paint();
        rectFPaint.setColor(Color.GREEN);
        rectFPaint.setAntiAlias(true);

        trianglePaint = new Paint();
        trianglePaint.setColor(Color.BLUE);
        trianglePaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(Math.min(width, height), Math.min(width, height));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();
        if (this.shap.equals(Shap.CIRCLE)) {
            canvas.drawCircle(width / 2, height / 2, height / 2, circlePaint);
        } else if (this.shap.equals(Shap.RECTF)) {
            RectF rectF = new RectF(0, 0, width, height);
            canvas.drawRect(rectF, rectFPaint);
        } else if (this.shap.equals(Shap.TRIANGLE)) {
            Path path = new Path();
            path.moveTo(0, 0);
            path.lineTo(width, 0);
            path.lineTo(width / 2, height);
            path.close();
            canvas.drawPath(path, trianglePaint);
        }
    }

    public void exchange() {
        if (this.shap.equals(Shap.CIRCLE)) {
            this.shap = Shap.RECTF;
        } else if (this.shap.equals(Shap.RECTF)) {
            this.shap = Shap.TRIANGLE;
        } else if (this.shap.equals(Shap.TRIANGLE)) {
            this.shap = Shap.CIRCLE;
        }
        invalidate();
    }

    enum Shap {
        CIRCLE, RECTF, TRIANGLE
    }

    public Shap getShap() {
        return shap;
    }
}
